package com.demo.controller;


import com.demo.clients.OrderClient;
import com.demo.entity.Order;
import com.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @author 韦存滨
 * @className UserController
 * @description
 * @date 2022年11月28日 23:29
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private OrderClient orderClient;
    //@Autowired
    //private RestTemplate restTemplate;

    @RequestMapping("{userId}")
    public User findById(@PathVariable String userId) {
        //获取购买的商品信息（远程调用商品微服务去获取商品信息） 发送http请求
        //java 发送http请求，使用apache httpclient
        // spring resttemplate  简洁
        //String url = "http://localhost:10000/order/12";//硬编码ip和端口
        String url = "http://cloud-order/order/12";  //硬编码服务名
        //Order[] orders = restTemplate.getForObject(url, Order[].class);
        Order[] orders = orderClient.listByUserId("12");
        User user = new User(userId, "wei", Arrays.asList(orders.clone()));
        return user;
    }
}